import {API} from '@/global';

class Utils {
	log(t = '', e = 'console.log', a = 'primary') {
		const type: any = {
			primary: '#57a3f3',
			success: '#19be6b',
			error: '#ed4014',
			warning: '#ffad33',
		};
		console.log(`%c ${e} %c ${t} %c`, 'background:#35495E; padding: 1px; border-radius: 3px 0 0 3px; color: #fff;', `background:${type[a]}; padding: 1px; border-radius: 0 3px 3px 0;  color: #fff;`, 'background:transparent');
	}

	formVerify(form: { [x: string]: null }, rules: any) {
		for (const res of rules) {
			if (form[res.field] === '' || form[res.field] === undefined || form[res.field] === null) {
				return res.msg;
			}
		}
		return false;
	}

	formatTime(time: string, date = false) {
		if (!time) return '';
		let formatDate = time.split('T').join(' ').split('.')[0];
		return date ? formatDate.split(' ')[0] : formatDate;
	}

	now() {
		return new Date().toLocaleDateString().split('/').join('-');
	}

	pad2(n: string | number) {
		return n < 10 ? '0' + n : n;
	}

	pad4(n: number) {
		return n < 1000 ? JSON.stringify(n).padStart(4 - JSON.stringify(n).length, '0') : n;
	}

	getFullTime(date: Date) {
		return date.getFullYear().toString() + '-' + this.pad2(date.getMonth() + 1) + '-' + this.pad2(date.getDate()) + ' ' + this.pad2(date.getHours()) + ':' + this.pad2(date.getMinutes());
	}

	assign(form: any, copyForm: any) {
		const _copyForm = JSON.parse(JSON.stringify(copyForm));
		const _keys = Object.keys(form);
		_keys.forEach((res) => {
			form[res] = _copyForm[res];
		});
		return form;
	}

	cacheTime(callback: any, time: number) {
		setTimeout(() => {
			callback();
		}, time);
	}

	setStorage(key: string, value: any) {
		localStorage.setItem(key, JSON.stringify(value));
	}

	getStorage(key: string, defaults: any = {}) {
		return JSON.parse(localStorage.getItem(key) || JSON.stringify(defaults));
	}


	groupArray = (arr: Array<any>, size: number) => {
		const result = [];
		for (let i = 0; i < arr.length; i += size) {
			result.push(arr.slice(i, i + size));
		}
		return result;
	}

	// 金额转大写
	digitUppercase(n: number) {
		const fraction = ['角', '分'];
		const digit = [
			'零',
			'壹',
			'贰',
			'叁',
			'肆',
			'伍',
			'陆',
			'柒',
			'捌',
			'玖',
		];
		const unit = [
			['元', '万', '亿'],
			['', '拾', '佰', '仟'],
		];
		const head = n < 0 ? '欠' : '';
		n = Math.abs(n);

		let s = '';

		for (let i = 0; i < fraction.length; i++) {
			s += (digit[Math.floor(this.multiply(n, 10 * Math.pow(10, i)) % 10)] + fraction[i]).replace(/零./, '');
		}
		s = s || '整';
		n = Math.floor(n);

		for (let i = 0; i < unit[0].length && n > 0; i++) {
			let p = '';
			for (let j = 0; j < unit[1].length && n > 0; j++) {
				p = digit[n % 10] + unit[1][j] + p;
				n = Math.floor(n / 10);
			}
			s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
		}
		return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
	}

	multiply(a: number, b: number) {
		let m = 0;
		const s1 = a.toString();
		const s2 = b.toString();
		try {
			m += s1.split('.')[1].length;
		} catch (e) {
		}
		try {
			m += s2.split('.')[1].length;
		} catch (e) {
		}
		return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m);
	}

	downloadFile(data: Blob, name: string) {
		const url = window.URL.createObjectURL(data);
		const link = document.createElement('a');
		link.style.display = 'none';
		link.href = url;
		link.setAttribute('download', name);
		link.setAttribute('target', '_blank');
		document.body.appendChild(link);
		link.click();
		document.body.removeChild(link);
	}

}

export default new Utils();
